using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;

namespace KFGame
{


    public class KFLog
    {

        private static KFLog _log;

        //记录名
        private string _file;

        //路径
        public string path
        {
            get
            {
                string pt = Application.streamingAssetsPath + "/KFLog/";
                if (!Directory.Exists(pt))
                    Directory.CreateDirectory(pt);

                return pt;
            }
        }
        //是间日期
        private string daytime
        {
            get
            {
                return DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "";
            }
        }

        /// <summary>
        /// 记录文件
        /// </summary>
        /// <value></value>
        public string filepath
        {
            get
            {
                return path + _file + "_" + daytime + ".txt";
            }
        }

        public static KFLog log
        {
            get
            {
                if (_log == null)
                    _log = new KFLog("KFGame");
                return _log;
            }
        }

        public KFLog(string filename)
        {
            _file = filename;
            /// 注册事件，当Debug调用时，就会调用：
            Application.logMessageReceived += OnLogCallBack;
        }

        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="tag"></param>
        /// <param name="filename"></param>
        public static void Log(string msg, LogType tag = LogType.Log, string filename = null)
        {
            log.WriteLog(msg, tag, filename);
        }

        /// <summary>
        /// 格式化写记录
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="tag"></param>
        /// <param name="filename"></param>
        public void WriteLog(string msg, LogType tag = LogType.Log, string filename = null)
        {
            string f = filepath;
            if (filename != null)
                f = filename;

            string logStr = string.Format("[{0}] {1}：{2}\n", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), tag, msg);

            WriteFile(logStr, f);
        }

        /// <summary>
        /// 系统日志回调
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="stackTrace"></param>
        /// <param name="type"></param>
        private void OnLogCallBack(string condition, string stackTrace, LogType type)
        {
            string logStr = string.Empty;
            switch (type)
            {
                case LogType.Log:
                    {
                        logStr = string.Format("[{0}]  {1}：{2}\n", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), type, condition);
                    }
                    break;
                case LogType.Assert:
                case LogType.Warning:
                case LogType.Exception:
                case LogType.Error:
                    {
                        if (string.IsNullOrEmpty(stackTrace))
                        {
                            // 发布到对应平台后，调用堆栈获取不到。使用 Environment.StackTrace 获取调用堆栈
                            logStr = string.Format("{0}：{1}\n{2}", type, condition, Environment.StackTrace);
                        }
                        else
                        {
                            logStr = string.Format("{0}：{1}\n{2}", type, condition, stackTrace);
                        }
                    }
                    break;
            }
            logStr += "\n";

            WriteFile(logStr, filepath);
        }

        /// <summary>
        /// 根据路径创建日记文件，并注册文件写入的函数。
        /// </summary>
        public void WriteFile(string msg, string filepath)
        {


            var filestream = File.Open(filepath, FileMode.Append | FileMode.OpenOrCreate);
            using (StreamWriter sw = new StreamWriter(filestream))
            {
                sw.WriteLine(msg);
                sw.Close();
                filestream.Close();
            }

        }

        /// <summary>
        /// 输出系统/硬件等一些信息
        /// </summary>
        public void OutputSystemInfo()
        {
            string str2 = string.Format("日志记录开始时间: {0}, 版本: {1}.", DateTime.Now.ToString(), Application.unityVersion);

            string systemInfo = SystemInfo.operatingSystem + " "
                                + SystemInfo.processorType + " " + SystemInfo.processorCount + " "
                                + "存储容量:" + SystemInfo.systemMemorySize + " "
                                + "图形设备: " + SystemInfo.graphicsDeviceName + " 供应商: " + SystemInfo.graphicsDeviceVendor
                                + " 存储容量: " + SystemInfo.graphicsMemorySize + " " + SystemInfo.graphicsDeviceVersion;

            Debug.Log(string.Format("{0}\n{1}", str2, systemInfo));
        }


    }
}